// pages/home/home.js
import {islogin} from '../../utils/islogin';
import {more} from '../../utils/more'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    choose: false,
    delBtnWidth:280,
    isScroll:true,
    windowHeight:0,
    index:0,
    text:'',
    data:[],
    inputShowed: false,
    inputVal: "",
    zudui:0,
  },

 /*搜索框*/
  content:function(e){
  var that = this;
  that.setData({
    text:e.detail.value
  })
},

 lookfor(){
  var that = this
  wx.request({
    url: 'https://api.yidan.work:2004/search',
    method:'POST',
    header:{
      'content-type':'application/json'
    },
    data:{
      key: that.data.text
    },
    success:function(res){
      console.log(res)
      that.setData({
        data:res.data
      })
      
    },
    fail:function(res){
      console.log(res)
    }
  })
 },

  /*点击按钮变色以及筛选框消失*/

  changeColor_1(e){
    console.log(e)
    this.setData({
      index:e.currentTarget.dataset['index']
    })
    this.setData({
      choose:false
    })
    var that = this
    wx.request({
      url:'https://api.yidan.work:2004/post',
      //data:obj,
      method:'GET',
      header:{
        'content-type':'application/json'
      },
      success:function(res){
        console.log(res)
        that.setData({
          data:res.data
        })
        
      },
      fail:function(res){
        console.log(res)
      }
    })
  },

  changeColor_2(e){
    console.log(e)
    this.setData({
      index:e.currentTarget.dataset['index']
    })
    this.setData({
      choose:false
    })
  },

  /*弹出筛选以及筛选按钮变色*/

  show_req(e){
    this.setData({
      choose: true
    })
    this.setData({
      index:e.currentTarget.dataset['index']
    })
  },
  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.request({
      url:'https://api.yidan.work:2004/post',
      //data:obj,
      method:'GET',
      header:{
        'content-type':'application/json'
      },
      success:function(res){
        console.log(res.data)
        that.setData({
          data:res.data
        })
        for(var i=0;i<that.data.data.length;i++){
          var a="data["+i+"].post_restrict_sex"
          if(that.data.data[i].post_restrict_sex==1){
            that.setData({
              [a]:'男'
            })
          }
          if(that.data.data[i].post_restrict_sex==2){
            that.setData({
              [a]:'女'
            })
          }
          if(that.data.data[i].post_restrict_sex==3){
            that.setData({
              [a]:'不限'
            })
          }
        }
        
      },
      fail:function(res){
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /* 加入组队*/
  join: function(e){
    var num =e.currentTarget.dataset.id;
    var that = this;
    var user_id=wx.getStorageSync('user_id');
    if(islogin()){
      islogin()
    }else{
      
      if(that.data.data[num].user_id==user_id){
        wx.showModal({
          title:'温馨提示',
          content:'不能和自己组队哦',
          confirmText:'确认',
          cancelText:'取消',
      })}
      else{
        if(that.data.data[num].post_require_num-that.data.data[num].post_now_num<1){
          wx.showModal({
            title:'温馨提示',
            content:'此队已满，请选择其他',
            confirmText:'确定',
            cancelText:'取消',
        })
        }else{
        wx.navigateTo({
            url:'/pages/joinPage/joinPage',
        })
        wx.request({
          url: 'https://api.yidan.work:2004/apply',
          header:{'content-type':'application/JSON'},
          method: 'POST',
          data:{
            user_id:wx.getStorageSync('user_id'),
            post_id: that.data.data[num].post_id
          },
          success:(res)=>{
            console.log(res.data)
          },
          fail:(res)=>{
            console.log(res.data)
          }
        })
      }
     
    }}
  },

  /*举报*/  
  report:function(e){
    var num =e.currentTarget.dataset.id;
    var that = this;
    var user_id=wx.getStorageSync('user_id');
    if(islogin()){
      islogin()
    }
    else{
      if(that.data.data[num].user_id==user_id){
        wx.showModal({
          title:'温馨提示',
          content:'不能举报自己哦',
          confirmText:'确定',
          cancelText:'取消',
      })}
      else{
        wx.request({
          url: 'https://api.yidan.work:2004/black',
          header:{'content-type':'application/JSON'},
          method: 'POST',
          data:{
            black_nameid:wx.getStorageSync('user_id'),
            black_post_id:that.data.data[num].post_id
          },
          success:(res)=>{
            console.log(res.data)
          },
          fail:(res)=>{
            console.log(res.data)
          }
        })
      }
      // wx.navigateTo({
      //   url: '/pages/black/black',
      // })
    }
  },
  
  /*左滑*/
  drawStart: function (e) {
    // console.log("drawStart");  
    var touch = e.touches[0]
 
    for(var index in this.data.data) {
      var item = this.data.data[index]
      item.right = 0
    }
    this.setData({
      data: this.data.data,
      startX: touch.clientX,
    })
 
  },
  drawMove: function (e) {
    var touch = e.touches[0]
    var item = this.data.data[e.currentTarget.dataset.index]
    var disX = this.data.startX - touch.clientX
    
    if (disX >= 20) {
      if (disX > this.data.delBtnWidth) {
        disX = this.data.delBtnWidth
      }
      item.right = disX
      this.setData({
        isScroll: false,
        data: this.data.data
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        data: this.data.data
      })
    }
  },  
  drawEnd: function (e) {
    var item = this.data.data[e.currentTarget.dataset.index]
    if (item.right >= this.data.delBtnWidth/2) {
      item.right = this.data.delBtnWidth
      this.setData({
        isScroll: true,
        data: this.data.data,
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        data: this.data.data,
      })
    }
  },
  mores:function(e){
    var num =e.currentTarget.dataset.id;
    var that = this;
    var id= that.data.data[num].user_id;
    more(id);
  },
  onRefresh:function(){
    //导航条加载动画
    wx.showNavigationBarLoading()
    //loading 提示框
    wx.showLoading({
      title: 'Loading...',
    })
    console.log("下拉刷新啦");
    setTimeout(function () {
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh:function(){
    this.onRefresh();
  },

})